package com.example.boot1.conttoller;

import com.example.boot1.bean.*;
import com.example.boot1.service.VueOrderInfoService;
import com.example.boot1.service.VueOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class VueOrderController {
    @Autowired
    private VueOrderService vueOrderService;
    @Autowired
    private VueOrderInfoService vueOrderInfoService;

    @GetMapping("/vue/orderList")
    @ResponseBody
    public ResponseEntity<BackPageDataBean> orderList(HttpServletRequest request,
                                                     Integer page,
                                                     String orderTime,
                                                     String tableNum
                                                     ){
        ResponseEntity responseEntity = null;
        BackPageDataBean backPageDataBean = null;
        BackDataBean backDataBean = null;
        int pageOffSetNum = page * 10;
        String startTime = "";
        String endTime = "";

        if (null!= orderTime && !"[]".equals(orderTime)){
            String[] tmpArr = orderTime.substring(1,orderTime.length()-1).split(",");
            startTime = tmpArr[0].substring(1,tmpArr[0].length()-1);
            endTime = tmpArr[1].substring(1,tmpArr[1].length()-1);
        }

        String token = request.getHeader("Authorization");
        System.out.println("order当前页token: "+token);
        VueOrderRequestBean vueOrderRequestBean = new VueOrderRequestBean();
        vueOrderRequestBean.setPage(pageOffSetNum);
        vueOrderRequestBean.setEndTime(endTime);
        vueOrderRequestBean.setStartTime(startTime);
        vueOrderRequestBean.setTableNum(tableNum);
        List<VueOrder> ulist = vueOrderService.findOrderByCondition(vueOrderRequestBean);

        //token
        if ("cfl15829936517".equals(token)){
            if (null!= ulist &&  ulist.size()>0){
                backDataBean = new BackDataBean();
                backDataBean.setCode(200);
                backDataBean.setMsg("查询成功！");
                backDataBean.setResult(ulist);

                backPageDataBean = new BackPageDataBean(backDataBean);
                backPageDataBean.setPage(page);
                backPageDataBean.setTotal(vueOrderService.findAllTotalByCondition(vueOrderRequestBean));
                responseEntity = ResponseEntity.status(HttpStatus.OK).body(backPageDataBean);
            }else{
                backDataBean = new BackDataBean();
                backDataBean.setCode(2001);
                backDataBean.setMsg("没有数据！");
                backDataBean.setResult(null);

                backPageDataBean = new BackPageDataBean(backDataBean);
                responseEntity = ResponseEntity.status(HttpStatus.OK).body(backPageDataBean);
            }
        }else{
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("没有权限！");
            backDataBean.setResult(null);
            backPageDataBean = new BackPageDataBean(backDataBean);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backPageDataBean);
        }

        return responseEntity;
    }


    @GetMapping("/vue/orderInfo")
    @ResponseBody
    public ResponseEntity<BackDataBean> orderInfo(HttpServletRequest request, Integer id){
        ResponseEntity responseEntity = null;

        BackDataBean backDataBean = null;

        String token = request.getHeader("Authorization");
        System.out.println("order当前页token: "+token);

        List<VueOrderTimesBean> vueOrderInfos = vueOrderInfoService.findOrderInfoById(id);


        //为了前端按钮加载
        try{
            Thread.sleep(1000);
        }catch  (Exception e){

        }

        //token
        if ("cfl15829936517".equals(token)){
            if ( null!= vueOrderInfos ){
                backDataBean = new BackDataBean();
                backDataBean.setCode(200);
                backDataBean.setMsg("查询成功！");
                backDataBean.setResult(vueOrderInfos);

                responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
            }else{
                backDataBean = new BackDataBean();
                backDataBean.setCode(2001);
                backDataBean.setMsg("没有数据！");
                backDataBean.setResult(null);

                responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
            }
        }else{
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("没有权限！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backDataBean);
        }

        return responseEntity;
    }

}
